package com.doublefish.doublefishrobot;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.doublefish.doublefishrobot.ui.setting.LocalPdfReaderActivity;
import com.doublefish.doublefishrobot.utils.AppCacheUtil;

public class WelcomeActvivty extends AppCompatActivity {

    private ImageView iconImageView;

    private LinearLayout privacyLL;
    private TextView okTv;
    private TextView rejectTv;
    private TextView privacyTv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        initView();
    }


    private void initView() {

        iconImageView = findViewById(R.id.iv_icon);

        privacyLL = findViewById(R.id.ll_privacy);
        privacyTv = findViewById(R.id.tv_privacy);
        okTv = findViewById(R.id.tv_ok);
        rejectTv = findViewById(R.id.tv_reject);

        Glide.with(this)
                .load(R.mipmap.icon)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(10))) // 设置圆角半径
                .into(iconImageView);

        if (AppCacheUtil.getInstance().isDidReadPrivacy()) {
            privacyLL.setVisibility(View.GONE);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    toMainActivity();
                }
            }, 1000);

        } else {
            privacyLL.setVisibility(View.VISIBLE);
        }

        privacyTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toPrivacyActivity();
            }
        });

        okTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toMainActivity();
            }
        });

        rejectTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exitApp();
            }
        });
    }

    private void toPrivacyActivity() {
        String fileName = getString(R.string.privacy_file_name);
        String title = getString(R.string.privacy);
        Intent intent = LocalPdfReaderActivity.makeIntent(this, fileName, title);
        startActivity(intent);
    }

    private void toMainActivity() {
        AppCacheUtil.getInstance().setDidReadPrivacy(true);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);

        finish();
    }

    private void exitApp() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
